package com.kewill.jira.common;

import com.kewill.jira.dao.IIssueDao;
import com.kewill.jira.dao.IssueDaoImpl;
import com.kewill.jira.model.Issue;
import com.kewill.jira.model.ReleaseNoteIssue;
import com.kewill.jira.util.FileUtil;
import com.kewill.jira.util.HtmlUtil;
import com.kewill.jira.util.JiraSyncUtil;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.util.*;

/**
 * Created by YanJun on 2016/7/12.
 */
public class ReleaseNotesHandler {
    public static List<ReleaseNoteIssue> setReleaseNotesFromProject(VelocityContext context,String fixVersion) {

        IIssueDao issueDao = new IssueDaoImpl();
        List<Issue> issueList = issueDao.loadIssueByFixVersion(fixVersion);
        List<ReleaseNoteIssue> releaseNoteIssueList = new ArrayList<ReleaseNoteIssue>();
        int sno = 0;
        for(Issue issue:issueList){
            Map<String,String> customFieldMap = issue.getCustomFieldList().get(0);
            sno ++;
            ReleaseNoteIssue rnIssue = new ReleaseNoteIssue();
            rnIssue.setSno(sno);
            rnIssue.setKey(issue.getKey());
            rnIssue.setKsNo("");
            rnIssue.setFpNo("");
            rnIssue.setCustRefNo("");
            rnIssue.setCustomer("");
            rnIssue.setCategory("");
            rnIssue.setComponent(JiraSyncUtil.getComponentStr(issue));
            rnIssue.setSummary(HtmlUtil.encodeHtml(issue.getSummary()));
            rnIssue.setDescription(HtmlUtil.encodeHtml(issue.getDescription()));
            rnIssue.setReleaseNote("");
            rnIssue.setImpact("");
            rnIssue.setQaName("");
            rnIssue.setRsFilePath("");
            rnIssue.setFsFilePath("");
            rnIssue.setBugCause("");
            releaseNoteIssueList.add(rnIssue);
        }
        context.put("vname",fixVersion);
        context.put("releaseNotes",releaseNoteIssueList);
        return releaseNoteIssueList;
    }

    public static void prepareTar(String version,List<ReleaseNoteIssue> releaseNoteIssueList) {
        File releasedTARDir = new File(TARGenerator.RELEASENOTE_TAR_DIR);
        if (releasedTARDir.exists()) {
            FileUtil.delDir(releasedTARDir);
            releasedTARDir.mkdirs();
        } else {
            releasedTARDir.mkdirs();
        }
        //for test
        String tarSvnUrl = JiraAttribute.TAR_SVN_URL + version + " ";
        String cmd = JiraAttribute.EXPORT_TAR_CMD + tarSvnUrl + releasedTARDir.getPath();
        //checkout hasn't been backuped tar
        JiraSyncUtil.execSvnExportTars(cmd);

        //move subdir's contents to releasedTAR dir, then remove sub dir
        File[] newTars = releasedTARDir.listFiles();
        for (int i = 0; i < newTars.length; i++) {
            if (newTars[i].isDirectory()) {
                FileUtil.mergeDir(newTars[i], releasedTARDir);
            }
        }

        List tarNameList = new ArrayList();
        for (Iterator iterator = releaseNoteIssueList.iterator(); iterator.hasNext(); ) {
            ReleaseNoteIssue issue = (ReleaseNoteIssue) iterator.next();
            tarNameList.add(issue.getKey().replace("KFF-", "TAR_").concat(".xls"));
        }

        //remove those tars not exist in release notes
        newTars = releasedTARDir.listFiles();
        List backupTarNameList = tarNameList;
        for (int i = 0; i < newTars.length; i++) {
            String tarName = newTars[i].getName().replace(" ", "");
            if (!tarNameList.contains(tarName)) {
                newTars[i].delete();
            } else {
                backupTarNameList.remove(tarName);
            }
        }




    }
}
